package site.higgs.limiter.config;

import site.higgs.limiter.interceptor.ArgumentInjecter;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 公共的入参注入器
 * </p>
 *
 * @author huangzy
 * @since 2023/4/7
 */
public class CommonInjecter implements ArgumentInjecter {

    @Override
    public Map<String, Object> inject(Method method, Object... args) {
        Map<String,Object> map = new HashMap<>();
        //遍历方法中的参数类型
        for (int i = 0; i < method.getParameterTypes().length; i++) {
            Class<?> parameterType = method.getParameterTypes()[i];
            String paramName = method.getParameters()[i].getName();
            Object object = args[i];
            if (object == null) {
                continue;
            }
            if (parameterType.isAssignableFrom(Collection.class) || parameterType.isAssignableFrom(Map.class)) {
                continue;
            }
            map.put(paramName,object);
        }
        return map;
    }
}
